<?php

namespace app\common\model\xilujob;

use think\db\Expression;
use think\Exception;
use think\Hook;
use think\Model;
use function fast\array_except;
use function fast\array_get;


class PackageOrder extends Model
{
    // 表名
    protected $name = 'xilujob_package_order';

    protected $autoWriteTimestamp = 'int';

    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    protected $append = [
        'pay_status_text',
        'pay_type_text'
    ];

    public function getPayStatusList(){
        return ['0' => __('已取消'), '1' => __('待支付'),'2'=>__('已完成')];
    }

    public function getTicketTypeList(){
        return ['1' => __('普票'), '2' => __('专票')];
    }

    public function getPayTypeList(){
        return ['1' => __('微信支付'), '2' => __('支付宝支付'),'3'=>__('线下支付')];
    }

    public function getPayStatusTextAttr($value,$data){
        $value = $value ? $value : (isset($data['pay_status']) ? $data['pay_status'] : '');
        $list = $this->getPayStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getPayTypeTextAttr($value,$data){
        $value = $value ? $value : (isset($data['pay_type']) ? $data['pay_type'] : '');
        $list = $this->getPayTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    /**
     * 创建订单
     * @param $params
     * @param $user_id
     * @return PackageOrder|array
     * @throws Exception
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public static function createOrder($params,$company_id,$user_id) {
        $package_id = array_get($params,'package_id');
        if(!$package_id){
            throw new Exception("非法参数");
        }
        $package = Package::get($package_id);
        if(!$package || $package->status == '0'){
            throw new Exception("套餐不存在或已下架");
        }
        $pay_type = array_get($params,'pay_type','1');
        #订单创建
        if($pay_type != 3){
            $params = array_except($params, ['corporate_company','open_bank','corporate_account']);
        }
        $data = array_merge([
            'user_id'       =>  $user_id,
            'company_id'    =>  $company_id,
            'order_no'      =>  "P".date("YmdHis").mt_rand(10,9999),
            'total_price'   =>  $package['salesprice'],
            'pay_price'     =>  $package['salesprice'],
            'pay_status'    =>  1,
            'package_name'      =>  $package['name'],
            'package_salesprice'=>  $package['salesprice'],
            'package_expires'    =>  $package['expires'],
            'package_unit'      =>  $package['unit'],
            'job_publish_num'   =>  $package['job_publish_num'],
            'resume_view_num'   =>  $package['resume_view_num'],
            'interview_num'     =>  $package['interview_num'],
            'job_refresh_num'   =>  $package['job_refresh_num'],
            'ip'            =>  request()->ip()
        ],$params);
        $result = self::create($data);
        if($result !== false){
            return $result;
        }else {
            throw new Exception("订单创建失败");
        }
    }
    public static function pay_notify($order_no,$trade_no){
        $order = self::where('order_trade_no',$order_no)->find();
        if(!$order){
            throw new Exception('订单不存在');
        }
        if($order->pay_status == '2'){
            throw new Exception('订单已支付完毕');
        }
        #更新订单状态
        $order->trade_no = $trade_no;
        $order->pay_status = '2';
        $order->paytime = time();
        $order->save();
        #更新企业可用数量
        $enterprise = EnterpriseInfo::where('company_id',$order->company_id)->find();
        $enterpriseData = [
            'resume_action_num'=>new Expression("resume_action_num+".$order->resume_view_num),
            'job_num_limit'=>new Expression("job_num_limit+".$order->job_publish_num),
            'interview_num'=>new Expression("interview_num+".$order->interview_num),
            'job_refresh_num'=>new Expression("job_refresh_num+".$order->job_refresh_num),
        ];
        $enterprise->allowField(true)->save($enterpriseData);
        return $order;
    }

}
